// // 改变主题
// export function handleThemeStyle(theme) {
//   document.documentElement.style.setProperty('--el-color-primary', theme)
//   for (let i = 1; i <= 9; i++) {
//     document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, `${getLightColor(theme, i / 10)}`)
//   }
//   for (let i = 1; i <= 9; i++) {
//     document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, `${getDarkColor(theme, i / 10)}`)
//   }
// }
// // 变浅颜色值
// export function getLightColor(color, level) {
//   let rgb = hexToRgb(color)
//   for (let i = 0; i < 3; i++) {
//     rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i])
//   }
//   return rgbToHex(rgb[0], rgb[1], rgb[2])
// }

// // 变深颜色值
// export function getDarkColor(color, level) {
//   let rgb = hexToRgb(color)
//   for (let i = 0; i < 3; i++) {
//     rgb[i] = Math.floor(rgb[i] * (1 - level))
//   }
//   return rgbToHex(rgb[0], rgb[1], rgb[2])
// }
// // hex颜色转rgb颜色
// export function hexToRgb(str) {
//   str = str.replace('#', '')
//   let hexs = str.match(/../g)
//   for (let i = 0; i < 3; i++) {
//     hexs[i] = parseInt(hexs[i], 16)
//   }
//   return hexs
// }

// // rgb颜色转Hex颜色
// export function rgbToHex(r, g, b) {
//   let hexs = [r.toString(16), g.toString(16), b.toString(16)]
//   for (let i = 0; i < 3; i++) {
//     if (hexs[i].length == 1) {
//       hexs[i] = `0${hexs[i]}`
//     }
//   }
//   return `#${hexs.join('')}`
// }

export function handleThemeStyle(color) {
  const isDark = localStorage.getItem('vueuse-color-scheme')
  const el = document.documentElement;
  const body = document.querySelector("body");
  el.style.setProperty("--el-color-primary", color);
  body.style.setProperty("--van-primary-color", color);
  // 判断主题模式模式，可根据自身业务调整代码
  let mixColor = isDark == 'dark' ? "#000000" : "#ffffff";
  for (let i = 1; i < 10; i++) {
    el.style.setProperty(`--el-color-primary-light-${i}`, colourBlend(color, mixColor, i / 10));
    el.style.setProperty(`--el-color-primary-dark-${i}`, colourBlend(color, mixColor, i / 10));
  }
  el.style.setProperty(`--el-color-primary-dark-2`, colourBlend(color, mixColor, 0.2));
}

export function colourBlend(c1, c2, ratio) {
  ratio = Math.max(Math.min(Number(ratio), 1), 0)
  let r1 = parseInt(c1.substring(1, 3), 16)
  let g1 = parseInt(c1.substring(3, 5), 16)
  let b1 = parseInt(c1.substring(5, 7), 16)
  let r2 = parseInt(c2.substring(1, 3), 16)
  let g2 = parseInt(c2.substring(3, 5), 16)
  let b2 = parseInt(c2.substring(5, 7), 16)
  let r = Math.round(r1 * (1 - ratio) + r2 * ratio)
  let g = Math.round(g1 * (1 - ratio) + g2 * ratio)
  let b = Math.round(b1 * (1 - ratio) + b2 * ratio)
  r = ('0' + (r || 0).toString(16)).slice(-2)
  g = ('0' + (g || 0).toString(16)).slice(-2)
  b = ('0' + (b || 0).toString(16)).slice(-2)
  return '#' + r + g + b
}
